package com.gwtexp.client;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.SplitLayoutPanel;
import com.gwtexp.client.mvp.AppActivityMapper;
import com.gwtexp.client.mvp.AppPlaceHistoryMapper;
import com.gwtexp.client.place.HelloPlace;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GWTexp implements EntryPoint {
	private final Place defaultPlace = new HelloPlace("World!");

	interface Binder extends UiBinder<SplitLayoutPanel, GWTexp> {
	}

	private static final Binder binder = GWT.create(Binder.class);
	@UiField
	SimplePanel appWidget;
	@UiField
	Label navArea;

	@Override
	public void onModuleLoad() {

		// @UiField TopPanel topPanel;
		// @UiField MailList mailList;
		// @UiField MailDetail mailDetail;
		// @UiField Shortcuts shortcuts;
		// Create the UI defined in Mail.ui.xml.
		SplitLayoutPanel outer = binder.createAndBindUi(this);

		ClientFactory clientFactory = GWT.create(ClientFactory.class);
		EventBus eventBus = clientFactory.getEventBus();
		PlaceController placeController = clientFactory.getPlaceController();

		// Add the outer panel to the RootLayoutPanel, so that it will be
		// displayed.
		RootLayoutPanel root = RootLayoutPanel.get();
		root.add(outer);

		// Start ActivityManager for the main widget with our ActivityMapper
		ActivityMapper activityMapper = new AppActivityMapper(clientFactory);
		ActivityManager activityManager = new ActivityManager(activityMapper,
				eventBus);
		activityManager.setDisplay(appWidget);

		// Start PlaceHistoryHandler with our PlaceHistoryMapper
		AppPlaceHistoryMapper historyMapper = GWT
				.create(AppPlaceHistoryMapper.class);
		PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(
				historyMapper);
		historyHandler.register(placeController, eventBus, defaultPlace);
		// Goes to the place represented on URL else default place
		historyHandler.handleCurrentHistory();
		navArea.setWidth("200px");

	}
}
